package com.esirong.whatime.ui.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.Chronometer;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 倒计时控控件
 * Created by esirong on 2015/11/11.
 */
public class Anticlockwise extends Chronometer {
    public Anticlockwise(Context context, AttributeSet attrs) {
        super(context, attrs);

        mTimeFormat = new SimpleDateFormat("mm:ss");
        this.setOnChronometerTickListener(listener);
    }

    private long mTime;
    private long mNextTime;
    private OnTimeCompleteListener mListener;
    private SimpleDateFormat mTimeFormat;
    private boolean isTimerComplete = false;
    private boolean isTimerOnPause = false;

    public Anticlockwise(Context context) {
        super(context);

    }

    //重新启动计时
    public void reStartTimer(long _time_s) {
        if (_time_s == -1) {
            mNextTime = mTime;
        } else {
            mTime = mNextTime = _time_s;
        }
        startTimer();
    }

    public void reStartTimer() {
        reStartTimer(-1);
    }

    //不建议方法名用onResume()或onPause()，容易和activity生命周期混淆
    //继续计时
    public void resumeTimer() {
        this.start();
        isTimerOnPause = false;
    }

    //暂停计时
    public void pauseTimer() {
        this.stop();
        isTimerOnPause = true;
    }

    public void startTimer() {
        this.start();
        isTimerOnPause = false;
        isTimerComplete = false;
    }

    public void stopTimer() {
        this.stop();
        isTimerComplete = true;
    }

    public boolean isTimerComplete() {
        return isTimerComplete;
    }

    public boolean isTimerPause() {
        return isTimerOnPause;
    }

    /**
     * 设置时间格式
     *
     * @param pattern 计时格式
     */
    public void setTimeFormat(String pattern) {
        mTimeFormat = new SimpleDateFormat(pattern);
    }

    public void setOnTimeCompleteListener(OnTimeCompleteListener l) {
        mListener = l;
    }

    OnChronometerTickListener listener = new OnChronometerTickListener() {
        @Override
        public void onChronometerTick(Chronometer chronometer) {
            if (mNextTime <= 0) {
                if (mNextTime == 0) {
                    Anticlockwise.this.stop();
                    isTimerComplete = true;
                    if (null != mListener)
                        mListener.onTimeComplete();
                }
                mNextTime = 0;
                updateTimeText();
                return;
            }

            mNextTime--;

            updateTimeText();
        }
    };

    //初始化时间(秒)
    public void initTime(long _time_s) {
        mTime = mNextTime = _time_s;
        updateTimeText();
    }

    //初始化时间（分秒）
    public void initTime(long _time_m, long _time_s) {
        initTime(_time_m * 60 + _time_s);
    }

    private void updateTimeText() {
        this.setText(mTimeFormat.format(new Date(mNextTime * 1000)));
    }

    public interface OnTimeCompleteListener {
        void onTimeComplete();
    }

}
